﻿/* 
 Copyright (C) 2011  Amblox Project

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Data;
using Amblox.Database;
using Amblox.Managers;

namespace Amblox.Game
{
    public class Messenger
    {
        public Habbo Habbo;

        public Dictionary<int, MessengerFriend> Friends;
        public Dictionary<int, MessengerRequest> Requests;
        public List<int> Updates;

        public Messenger(Habbo Habbo)
        {
            this.Habbo = Habbo;

            LoadData();
        }

        public void LoadData()
        {
            this.Friends = new Dictionary<int, MessengerFriend>();
            this.Requests = new Dictionary<int, MessengerRequest>();
            this.Updates = new List<int>();

            DataTable Friends1;
            DataTable Friends2;
            DataTable RequestsT;

            using (DatabaseClient dbClient = Environment.dbManager.GetClient())
            {
                Friends1 = dbClient.ReadDataTable("SELECT userid FROM messenger_friends WHERE friendid = " + Habbo.ID);
                Friends2 = dbClient.ReadDataTable("SELECT friendid FROM messenger_friends WHERE userid = " + Habbo.ID);
                RequestsT = dbClient.ReadDataTable("SELECT user_from FROM messenger_requests WHERE user_to = " + Habbo.ID);
            }

            foreach (DataRow dRow in Friends1.Rows)
            {
                if (!Friends.ContainsKey((int)dRow["userid"]))
                    Friends.Add((int)dRow["userid"], new MessengerFriend(HabboManager.GetHabbo((int)dRow["userid"])));
            }

            foreach (DataRow dRow in Friends2.Rows)
            {
                if(!Friends.ContainsKey((int)dRow["friendid"]))
                    Friends.Add((int)dRow["friendid"], new MessengerFriend(HabboManager.GetHabbo((int)dRow["friendid"])));
            }

            foreach (DataRow dRow in RequestsT.Rows)
            {
                if (!Requests.ContainsKey((int)dRow["user_from"])) 
                    Requests.Add((int)dRow["user_from"], new MessengerRequest((int)dRow["user_from"], Habbo.ID));
            }

            MyStatusChange();
        }

        public void MyStatusChange()
        {
            foreach (int FriendID in Friends.Keys)
            {
                Habbo Friend = HabboManager.GetHabbo(FriendID);

                if (Friend == null || Friend.Client == null || Friend.Client.Messenger == null)
                    continue;

                Friend.Client.Messenger.UpdateFriend(Habbo.ID);
            }
        }

        public void AcceptRequest(int RequestID)
        {
            if (!Requests.ContainsKey(RequestID))
                return;

            Requests.Remove(RequestID);
            Friends.Add(RequestID, new MessengerFriend(HabboManager.GetHabbo(RequestID)));

            UpdateFriend(RequestID);

            using (DatabaseClient dbClient = Environment.dbManager.GetClient())
                dbClient.ExecuteQuery("DELETE FROM messenger_requests WHERE user_from = " + RequestID + " AND user_to = " + Habbo.ID + "; INSERT INTO messenger_friends (userid, friendid) VALUES (" + RequestID + ", " + Habbo.ID + ")");
        }

        public void RefuseRequest(int RequestID)
        {
            if (!Requests.ContainsKey(RequestID))
                return;

            Requests.Remove(RequestID);

            using (DatabaseClient dbClient = Environment.dbManager.GetClient())
                dbClient.ExecuteQuery("DELETE FROM messenger_requests WHERE user_from = " + RequestID + " AND user_to = " + Habbo.ID);
        }

        public void SendUpdates()
        {
            if (!(Habbo != null && Habbo.Client != null && Habbo.Client.Connection != null))
                return;

            FuseBuilder UpdatesF = new FuseBuilder(13);
            UpdatesF.AppendWired(0);
            UpdatesF.AppendWired(Updates.Count);
            UpdatesF.AppendWired(0);

            foreach (int FriendID in Updates)
            {
                MessengerFriend Friend = Friends[FriendID];
                UpdatesF.Append(Friend.Serialize(false));
                UpdatesF.AppendBoolean(false);
            }

            Updates.Clear();

            Habbo.Client.Connection.SendData(UpdatesF);
        }

        public void UpdateFriend(int FriendID)
        {
            if (!Friends.ContainsKey(FriendID))
                return;

            Updates.Add(FriendID);
            SendUpdates();
        }

        public FuseBuilder SerializeFriends()
        {
            FuseBuilder FriendsF = new FuseBuilder(12);
            FriendsF.AppendWired(5000);
            FriendsF.AppendWired(200);
            FriendsF.AppendWired(5000);
            FriendsF.AppendWired(900);
            FriendsF.AppendBoolean(false);
            FriendsF.AppendWired(Friends.Count);

            foreach (MessengerFriend Friend in Friends.Values)
                FriendsF.Append(Friend.Serialize(false));

            return FriendsF;
        }

        public FuseBuilder SerializeRequests()
        {
            FuseBuilder RequestsF = new FuseBuilder(314);
            RequestsF.AppendWired(Requests.Count);
            RequestsF.AppendWired(Requests.Count);

            foreach (MessengerRequest Request in Requests.Values)
                RequestsF.Append(Request.Serialize());

            return RequestsF;
        }
    }
}
